package com.powernode.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.anno.MyLog;
import com.powernode.domain.SysUser;
import com.powernode.service.SysUserService;
import com.powernode.utils.AuthUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 内容:
 * 作者: cy
 */
@RestController
@Api(tags = "管理员接口")
@RequestMapping("sys/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("info")
    @ApiOperation("查询当前登录用户信息")
    public ResponseEntity<SysUser> getCurrentUserInfo() {
        Long userId = AuthUtil.getUserIdOrShopId(true);
        SysUser user = sysUserService.getById(userId);
        return ResponseEntity.ok(user);
    }

    @GetMapping("page")
    @ApiOperation("分页查询管理员列表")
    @PreAuthorize("hasAuthority('sys:user:page')")
    @MyLog(operation = "分页查询管理员列表")
    public ResponseEntity<Page<SysUser>> loadSysUserPage(Page<SysUser> page, SysUser sysUser) {
        Page<SysUser> sysUserPage = sysUserService.selectSysUserPage(page, sysUser);
        return ResponseEntity.ok(sysUserPage);
    }


    @PostMapping
    @ApiOperation("新增管理员")
    @PreAuthorize("hasAuthority('sys:user:save')")
    @MyLog(operation = "新增管理员")
    public ResponseEntity<Void> addSysUser(@RequestBody SysUser sysUser) {
        sysUserService.save(sysUser);
        return ResponseEntity.ok().build();
    }

}
